// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Music, {musicName} from "../PublicScript/Music";
import GameData_red_blue_man, {specialType} from "./GameData_red_blue_man";
import GameControl_red_blue_man from "./GameControl_red_blue_man";
import PlaySpecialMusic from "./PlaySpecialMusic";

const {ccclass, property} = cc._decorator;

export enum fluidType{
    fire ,
    water ,
    venom,
    ice,
}

@ccclass
export default class Fluid extends cc.Component {

    @property(cc.SpriteFrame)
    fluidSpriteFrame: cc.SpriteFrame[] = [];
    @property({type: cc.Enum(fluidType)})
    type: fluidType = fluidType.fire;
    @property({tooltip:"做出反应的节点", type: cc.Node})
    eventNode: cc.Node = null;
    @property({tooltip: "标签"})
    tagNum: string = "";
    //
    nodeList = [];
    nodeLength = 0;
    //头所在位置
    headPos = 0;
    //尾所在位置
    endPos = 0;
    //保存初始的大小
    initSize = 0;

    onLoad () {
        this.nodeList = this.node.children;
        this.nodeLength = this.nodeList.length;
        this.headPos = this.nodeList[0].x;
        this.endPos = this.nodeList[this.nodeLength - 1].x;
        this.initSize = this.node.width;
    }

    start () {
        this.init();
    }

    init(){
        if(this.type != fluidType.ice){
            for(let i of this.nodeList){
                i.getComponent(cc.Sprite).spriteFrame = this.fluidSpriteFrame[this.type];

            }
        }
        this.node.getComponent(cc.PhysicsBoxCollider).tag = this.type+1;

        if(this.type != fluidType.ice){
            let nodeWidthMax = this.node.x + this.node.width/2;
            let nodeWidthMin = this.node.x - this.node.width/2;
            let nodeHeightMax = this.node.y + this.node.height/2;
            let nodeHeightMin = this.node.y - this.node.height/2;

            GameData_red_blue_man.fluidStateList.push(false);
            let tag = GameData_red_blue_man.fluidStateList.length - 1;
            // console.log(this.tag);
            let special = this.node.addComponent(PlaySpecialMusic);
            special.setValue({name: musicName.water, widthMax: nodeWidthMax, widthMin: nodeWidthMin, HeightMax: nodeHeightMax, HeightMin: nodeHeightMin, type:specialType.fluid, tag: tag});
        }
    }

    //动画
    anim() {
        for(let i of this.nodeList){
            if(i.x - 2 <= this.headPos){
                i.x = this.endPos;
            }
            i.x -= 2;
        }
    }

    event(){
        Music.GamePlayEffect(musicName.dongjie);
        // GameData.fluidStateList[this.tag] = false;
        this.node.active = false;
        this.eventNode.active = true;
    }

    update (dt) {
        this.anim();
    }
}
